#include <iostream>
#include "signal.h"
using namespace std;

struct TTT {
    int a;
    int b;
    int c;
};

class N : public Box {
public:
    virtual void func_c(int a, int b) = 0;
};

class A : public N {
public:
    A()
    {
        s1.connect(this, &A::func_ii);
    }
    void func_b(int a) { cout << a << "aaaaaa\n"; }
    void func_c(int a, int b) { cout << a << "+" << b << "=" << a + b << std::endl; }
    void func_a()
    {
        TTT t = { 1, 's', 't' };
        s.eemit(t, t, t);
    }

    void func_i()
    {
        int* i = new int(1);
        s1.eemit(i);
    }

    void func_ii(int* i)
    {
        cout << *i;
        (*i)++;
        delete i;
    }

    void func_z()
    {
        cout << "zhou xiang " << std::endl;
    }

    Signal<void (*)(TTT, TTT, TTT)> s;
    Signal<void (*)(int*)> s1;
};

class B : public Box {
public:
    B(A* a)
        : m_a(a)
    {
    }
    B() {}
    void func_b(int a) { cout << a << "bbbbbbb\n"; }
    void func_slot(TTT t, TTT t2, TTT t3)
    {
        cout << t.a + t2.b + t3.c << "-==-=-=-==-=\n";
    }

    void func_z()
    {
        cout << "love chenchen" << std::endl;
    }

    A* m_a;
    void func_e()
    {
        m_a->s.connect(this, &B::func_slot);
    }
};

void* pt_callback(void* arg)
{
    A aa;
    B bb;
    Signal<void (*)()>* s = (Signal<void (*)()>*)(arg);
    s->connect(&aa, &A::func_z);
    s->connect(&bb, &B::func_z);
    EventLoop* loop = EventLoop::currentEventLoop();
    loop->exec();
    return NULL;
}

int main()
{
    A object;
    B object1(&object);

    Signal<void (*)(int, int)> s;

    Signal<void (*)()> ssss;

    // ssss.connect(&object, &A::func_z);
    // ssss.connect(&object1, &B::func_z);
    // ssss.connect(&object1, &B::func_z);

    s.connect(&object ,&A::func_c, AYNC);
    //    s.connect(&object ,&A::func_c);
    //    s.connect(&object ,&A::func_c);
    //    s.connect(&object ,&A::func_c);
    //    s.connect(&object ,&A::func_c);
    //    s.connect(&object ,&A::func_c);
    //    s.connect(&object ,&A::func_c);
    //    object1.func_e();
    //    object1.func_e();

    //    object.func_a();

    //ssss.eemit();
    // ssss.disconnect(&object, &A::func_z);
    // ssss.disconnect(&object1, &B::func_z);

    //object.func_i();

    cout << "---------------------------\n";

    //ssss.eemit();

    // s.eemit(4, 456);

    // EventLoop *loop = EventLoop::currentEventLoop();
    // loop->postNormalTask(NewNormalTask(&object, &A::func_b, 1314));
    //std::cout << "+++++++++++++++++++++\n";

    pthread_t ptid;

    int ptre = pthread_create(&ptid, NULL, pt_callback, (void*)(&ssss));
    if (ptre != 0) {
        printf("dispatcher thread create fail!\n");
    }

    sleep(10);
    ssss.eemit();
    // ssss.disconnect(&object, &A::func_z);
    // ssss.disconnect(&object1, &B::func_z);

    //object.func_i();

    // cout << "---------------------------\n";

    ssss.eemit();

    EventLoop* loop = EventLoop::currentEventLoop();
    loop->exec();

    return 0;
}
